// pages/fenxiao/team.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hiddenLoading: false,
        userInfo: {},
        team_type: 1,
        teamList: [],
        itemTitle: '智能排序',
        option2: [{
                text: '订单数量降序',
                value: '0'
            },
            {
                text: '注册时间升序',
                value: '1'
            },
            {
                text: '注册时间降序',
                value: '2'
            },
        ],
        sort: '0',
        searchValue: '',
        page: 1,
        rows: 10,
        totallPages: 0,
        is_shop: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        if (options.title) {
            wx.setNavigationBarTitle({
                title: options.title //修改title
            })
        }
        var that = this;
        if (typeof (options.type) != "undefined") {
            that.setData({
                team_type: options.type
            });
        }

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this
        this.setData({
            is_shop: wx.getStorageSync('is_shop')
        })
        //用户授权登录
        app.login(function (userInfo) {
            that.setData({
                userInfo: userInfo
            });
            that.getTeam();
        });
    },
    onDropdownChange(e) {
        this.setData({
            sort: e.detail,
            page: 1,
            teamList: []
        });
        this.getTeam();
    },
    onChange(e) {
        this.setData({
            searchValue: e.detail,
        });
    },
    onSearch() {
        console.log(this.data.searchValue)
        this.setData({
            page: 1,
            teamList: []
        })
        this.getTeam();
        this.selectComponent('#item').toggle(false);
        //Toast('搜索' + this.data.value);
    },
    //切换
    changeTeam: function (e) {
        var that = this;
        var otype = e.currentTarget.dataset.type
        that.setData({
            team_type: otype
        });
        that.getTeam();
    },

    //获取列表
    getTeam: function () {
        wx.showLoading({
            title: '加载中',
        })
        var that = this;
        var paraArr = new Array();
        paraArr['state'] = that.data.team_type;
        paraArr['user_id'] = that.data.userInfo.userID;
        // paraArr['user_id'] = 168;
        //164  that.data.userInfo.userID;
        paraArr['sort'] = that.data.sort;
        paraArr['search'] = that.data.searchValue;
        paraArr['page'] = that.data.page;
        paraArr['rows'] = that.data.rows;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api/fenxiao/getTeamList',
            data: {
                state: paraArr['state'],
                user_id: paraArr['user_id'],
                sort: paraArr['sort'],
                search: paraArr['search'],
                page: paraArr['page'],
                rows: paraArr['rows'],
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {

                let list = that.data.teamList.concat(res.data.data)
                that.setData({
                    teamList: list,
                    hiddenLoading: true,
                    totallPages: res.data.last_page
                });
            },
            complete: function () {
                wx.hideLoading()
            }
        })
    },
    on_phone(e) {
        if (this.data.is_shop >= 1) {
            wx.makePhoneCall({
                phoneNumber: e.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
            })
        }

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onReachBottom() {
        console.log(this.data.page, this.data.totallPages)
        if (this.data.totallPages && this.data.page < this.data.totallPages) {
            this.setData({
                page: this.data.page + 1
            })
            this.getTeam() // 调用列表接口
        } else if (this.data.totallPages) {
            wx.showToast({
                title: "没有更多了",
                icon: "none"
            })
        }
    },

    // onPullDownRefresh: function () {
    //   this.getTeam();
    //   this.setData({
    //     searchValue:'',
    //     page:1
    //   })
    //   wx.stopPullDownRefresh();
    // }

})